package com.dalin.platform.log.action;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.ResourceBundle;

import com.dalin.common.utils.FileUtils;
import com.dalin.framework.action.StrutsTwoAction;
/*****************************************************************************
 * @content: 数据库还原                                                                                                                           *
 * @author : 那建林                                                                                                                                 *
 * @crtDate: 2010-06-23                                                     *
 * @modDate: 2010-06-23                                                     *
 * @version: 1.00                                                           *
 ****************************************************************************/
public class SystemRegainAction extends StrutsTwoAction{
	private File uf;
	private String ufFileName;
	private String ufContentType;
	
    public String execute() {
        try {
			super.printInfo("", "===== this action begin");
			String absolutePath = super.getAbsolutePhysicalPath();
	        absolutePath += "fileInfo/temp";
	        FileUtils.unzipFile(uf,	new File(absolutePath));
	        regainData(absolutePath + "/databaseBackup.lin");
	        setResContent("还原成功!!!<SCRIPT type=\"text/javascript\">window.close();</SCRIPT>");
	        super.printInfo("", "===== this action end");
		} catch (Exception e) {
			e.printStackTrace();
		}
        return null;
    }
    
    private boolean regainData(String absolutePhysicalFilePath) {
		StringBuffer buf = new StringBuffer("imp ");
		ResourceBundle rb = ResourceBundle.getBundle("jdbc");
		String jdbcUrl = rb.getString("jdbc.url");
		int lastColon = jdbcUrl.lastIndexOf(":") + 1;
		String dbName = jdbcUrl.substring(lastColon, jdbcUrl.length());
		buf.append(rb.getString("jdbc.username")).append("/")
			.append(rb.getString("jdbc.password")).append("@")
			.append(dbName).append(" file=")
			.append(absolutePhysicalFilePath);
		try {
			Process process = Runtime.getRuntime().exec(buf.toString());
			BufferedReader b = new BufferedReader(
					new InputStreamReader(process.getErrorStream()));
			String line = null;
			while((line = b.readLine()) != null) {
				log.info(line);
			}
			int value  = process.exitValue();
			if (value != 0) { 
				log.error("执行命令" + buf.toString()
						+ "失败!  exit value =" + value);
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return true;
	}

    
    
    
	public File getUf() {
		return uf;
	}

	public void setUf(File uf) {
		this.uf = uf;
	}

	public String getUfFileName() {
		return ufFileName;
	}

	public void setUfFileName(String ufFileName) {
		this.ufFileName = ufFileName;
	}

	public String getUfContentType() {
		return ufContentType;
	}

	public void setUfContentType(String ufContentType) {
		this.ufContentType = ufContentType;
	}
}
